//
//  SmallDecisionsMhEditViewController.m
//  SmallDecisions
//
//  Created by 李付雄 on 2025/8/20.
//

#import "SmallDecisionsMhEditViewController.h"
#import "SmallDecisionsTextField.h"

@interface SmallDecisionsMhEditViewController ()
@property (nonatomic,strong) UIScrollView *scrollView;
@property (nonatomic,strong) NSMutableArray *luckys;
@property (nonatomic,strong) NSMutableArray *items;
@property (nonatomic,strong) UIButton *mhSureBtn;
@end

@implementation SmallDecisionsMhEditViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.edgesForExtendedLayout = UIRectEdgeNone;
    self.title = @"自定义";
    [self setSmallDecisionsItemsSubViews];
}

- (void)setSmallDecisionsItemsSubViews {
    for (NSUInteger i = 0,n = self.luckys.count; i < n; i ++) {
        NSString *value = [NSString stringWithFormat:@"%ld.",i + 1];
        SmallDecisionsTextField *item = [self setSmallDecisionsItemTextFieldWithTitle:value message:self.luckys[i]];
        [self.scrollView addSubview:item];
        SmallDecisionsTextField *lastItem = [self.items lastObject];
        [item mas_makeConstraints:^(MASConstraintMaker *make) {
            if(!lastItem) {
                make.top.equalTo(self.scrollView).offset(20);
            } else {
                make.top.equalTo(lastItem.mas_bottom).offset(10);
            }
            
            make.height.mas_equalTo(50);
            make.left.equalTo(self.view).offset(15);
            make.right.equalTo(self.view).offset(-15);
        }];
        [self.items addObject:item];
    }
    self.scrollView.contentSize = CGSizeMake(kScreenWidth, self.items.count * 60 + 80);
    
    [self mhSureBtn];
}

- (SmallDecisionsTextField *)setSmallDecisionsItemTextFieldWithTitle:(NSString *)title message:(NSString *)message{
    SmallDecisionsTextField *itemTextField = [[SmallDecisionsTextField alloc] init];
    itemTextField.placeholder = @"请输入";
    itemTextField.placeholderColor = [UIColor colorWithHexString:@"0x666666"];
    itemTextField.backgroundColor = SmallDecisionsSecondBgColor;
    itemTextField.layer.cornerRadius = 10;
    itemTextField.layer.masksToBounds = YES;
    itemTextField.text = message;
    itemTextField.leadingViewRect = CGRectMake(0, 0, 30, 50);
    [itemTextField setLeadingViewTitle:title font:SmallDecisionsSetFontSize(14) color:SmallDecisionsThemeFontColor];
    itemTextField.trailingViewRect = CGRectMake(0, 0, 40, 50);
    [itemTextField setTrailingViewTitle:@"删除" font:SmallDecisionsSetFontSize(14)  color:SmallDecisionsRedColor];
    itemTextField.trailingViewBtn.tag = [title intValue];
    [itemTextField.trailingViewBtn addTarget:self action:@selector(deleteDlpItemBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    return itemTextField;
}

- (void)deleteDlpItemBtnAction:(AlignButton *)btn {
    NSUInteger target = btn.tag - 1;
    if(self.items.count > 2) {
        SmallDecisionsTextField *textField = self.items[target];
        [textField removeFromSuperview];
        [self.items removeObjectAtIndex:target];
        self.scrollView.contentSize = CGSizeMake(kScreenWidth, self.items.count * 60 + 80);
    } else {
        [SmallDecisionsHUDHelper showMessage:@"至少需要两个选项"];
    }
}

- (void)mhSureBtnAction {
    NSMutableArray *messages = [[NSMutableArray alloc] init];
    for (NSUInteger i = 0,n = self.items.count; i < n; i ++) {
        SmallDecisionsTextField *item = self.items[i];
        [messages addObject:item.text];
    }
    if (self.callback) {
        self.callback(messages);
    }
    [SmallDecisionsNavigatorManager dismissViewControllerAnimated:YES completion:nil];
}

- (UIScrollView *)scrollView {
    if(!_scrollView) {
        _scrollView = [[UIScrollView alloc] init];
        _scrollView.alwaysBounceVertical = YES;
        [self.view addSubview:_scrollView];
        [_scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_offset(0);
            make.left.mas_offset(0);
            make.right.mas_offset(0);
            make.bottom.mas_offset(0);
        }];
    }
    return _scrollView;
}

- (UIButton *)mhSureBtn {
    if(!_mhSureBtn) {
        _mhSureBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_mhSureBtn setTitle:@"确定" forState:UIControlStateNormal];
        [_mhSureBtn setTitleColor:SmallDecisionsThemeFontColor forState:UIControlStateNormal];
        _mhSureBtn.backgroundColor = SmallDecisionsThemeColor;
        [_mhSureBtn addTarget:self action:@selector(mhSureBtnAction) forControlEvents:UIControlEventTouchUpInside];
        _mhSureBtn.layer.cornerRadius = 20;
        _mhSureBtn.layer.masksToBounds = YES;
        [self.view addSubview:_mhSureBtn];
        [_mhSureBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.view).offset(20);
            make.right.equalTo(self.view).offset(-20);
            make.height.mas_equalTo(45);
            make.bottom.equalTo(self.view.mas_safeAreaLayoutGuideBottom).offset(-10);
        }];
        
    }
    return _mhSureBtn;
}

- (NSMutableArray *)luckys {
    if(!_luckys) {
        _luckys = [[NSMutableArray alloc] initWithArray:self.list];
    }
    return _luckys;
}

- (NSMutableArray *)items {
    if(!_items) {
        _items = [[NSMutableArray alloc] init];
    }
    return _items;
}

- (UIView *)setLeftBarButton {
    UIButton *closeButton = [UIButton buttonWithType:UIButtonTypeCustom];
    closeButton.frame = CGRectMake(0, 0, 30, 30);
    closeButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    [closeButton setImage:[UIImage imageNamed:@"global_close"] forState:UIControlStateNormal];
    return closeButton;
}

- (void)leftBarButtonAction:(UIView *)view {
    [SmallDecisionsNavigatorManager dismissViewControllerAnimated:YES completion:nil];
}

- (UIView *)setRightBarButton {
    UIButton *rightBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    rightBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
    [rightBtn setTitle:@"添加" forState:UIControlStateNormal];
    rightBtn.titleLabel.font = SmallDecisionsSetBoldFontSize(14);
    [rightBtn setTitleColor:SmallDecisionsThemeFontColor forState:UIControlStateNormal];
    rightBtn.frame = CGRectMake(0, 0, 30, 30);
    return rightBtn;
}

- (void)rightBarButtonAction:(UIView *)view {
    NSString *title = [NSString stringWithFormat:@"%ld.",self.items.count + 1];
    SmallDecisionsTextField *item = [self setSmallDecisionsItemTextFieldWithTitle:title message:@""];
    [self.scrollView addSubview:item];
    SmallDecisionsTextField *lastItem = [self.items lastObject];
    [item mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(lastItem.mas_bottom).offset(10);
        make.height.mas_equalTo(50);
        make.left.equalTo(self.view).offset(15);
        make.right.equalTo(self.view).offset(-15);
    }];
    [self.items addObject:item];
    self.scrollView.contentSize = CGSizeMake(kScreenWidth, self.items.count * 60 + 80);
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(.35 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [self.scrollView scrollToBottom];
    });
}


@end
